package com.example.yskjproject.ui.fragment;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.yskjproject.adapter.RechargeRecordAdapter;
import com.example.yskjproject.adapter.WalletPagerAdapter;
import com.example.yskjproject.base.BaseFragment;
import com.example.yskjproject.databinding.PayFragmentBinding;
import com.example.yskjproject.inter.Refreshable;
import com.google.android.material.tabs.TabLayoutMediator;

public class WalletFragment extends BaseFragment<PayFragmentBinding> {
    private PayFragmentBinding mPayFragmentBinding;
    private WalletPagerAdapter mWalletPagerAdapter;

    @Override
    protected PayFragmentBinding onCreateViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup parent) {
        // 初始化 ViewBinding
        mPayFragmentBinding = PayFragmentBinding.inflate(inflater);

        // 初始化 ViewPager2 和 Adapter
        mWalletPagerAdapter = new WalletPagerAdapter(this);
        mPayFragmentBinding.viewPager.setAdapter(mWalletPagerAdapter);

        // 默认选中第一个页面
        mPayFragmentBinding.viewPager.setCurrentItem(0, false);

        // 绑定 TabLayout 和 ViewPager2
        new TabLayoutMediator(mPayFragmentBinding.tabLayout, mPayFragmentBinding.viewPager, (tab, position) -> {
            switch (position) {
                case 0:
                    tab.setText("全部");
                    break;
                case 1:
                    tab.setText("充值记录");
                    break;
                case 2:
                    tab.setText("提现记录");
                    break;
            }
        }).attach();
        mPayFragmentBinding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                Fragment currentFragment = getChildFragmentManager().findFragmentByTag("f" + position);
                if (currentFragment instanceof Refreshable) {
                    ((Refreshable) currentFragment).refreshContent();
                }
            }
        });

        return mPayFragmentBinding;
    }

    @Override
    protected void onData() {
        // 可以在这里进行数据加载操作
    }
}
